﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Moq;
using SportsStore.ServicesInterfaces;
using SportsStore.ServicesInterfaces.Interfaces;
using SportsStore.Web.Controllers;
using System.Web.Mvc;
using SportsStore.Web.Models;
using SportsStore.Model;
using SportsStore.Web.Models.Product;

namespace SportsStore.UnitTests.Controllers
{
    [TestFixture]
  public class ProductControllerTest : BaseControllerTests
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            _productServiceMock = new Mock<IProductService>();
            _controller = new ProductController(_productServiceMock.Object, null);

            _productServiceMock.Setup(p => p.GetProducts(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>())).Returns(pagedProducts);
        }

        #endregion

        private Mock<IProductService> _productServiceMock;
        private ProductController _controller;

        private static readonly List<Product> products = new List<Product>
                                                {
                                                    new Product { Id = 1, Name = "P1", Category = new Category() { Id = 1, Name = "c1"}},
                                                    new Product { Id = 2, Name = "P2", Category = new Category() { Id = 1, Name = "c1"}},
                                                    new Product { Id = 3, Name = "P3", Category = new Category() { Id = 1, Name = "c1"}},
                                                    new Product { Id = 4, Name = "P4", Category = new Category() { Id = 1, Name = "c1"}}
                                                };

      private readonly PagedResult<Product> pagedProducts = new PagedResult<Product>
                                                              {
                                                                CurrentPage = 1,
                                                                ItemsPerPage = 2,
                                                                Results = products,
                                                                TotalItems = products.Count()
                                                              };

      [Test]
      public void GivenAProductController_WhenIGoToTheIndexPageForAGivenCategory_ThenTheCorrectNumberOfPagesAreReturned()
      {
        var controller = new ProductController(_productServiceMock.Object, null);
        var result = (ViewResult)controller.Index("c1");

        var model = (ProductsViewModel)result.Model;

        Assert.That(model.PagingInfo.TotalPages, Is.EqualTo(2));
      }

      [Test]
        public void GivenAProductController_WhenIGoToTheIndexPage_ThenItReturnsTheCorrectNumberOfProducts()
        {
            var controller = new ProductController(_productServiceMock.Object, null);
            var result = (ViewResult)controller.Index(null);

            var model = (ProductsViewModel)result.Model;

            Assert.That(model.Products.Count(), Is.EqualTo(products.Count()));
        }

        [Test]
        public void GivenAProductController_WhenIGoToTheIndexPage_ThenItReturnsTheCorrectView()
        {
            var controller = new ProductController(_productServiceMock.Object, null);
            var result = (ViewResult)controller.Index(null);

            var model = (ProductsViewModel)result.Model;
          
            Assert.That(model.Products.Count(), Is.EqualTo(products.Count));
            Assert.That(model.PagingInfo.CurrentPage, Is.EqualTo(1));
            Assert.That(model.PagingInfo.ItemsPerPage, Is.EqualTo(2));
            Assert.That(model.PagingInfo.TotalItems, Is.EqualTo(4));
            Assert.That(model.PagingInfo.TotalPages, Is.EqualTo(2));
        }
    }
}
